package com.test.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.test.bean.QuartzTask;
import com.test.service.QuartzTaskService;

@Controller("quartzTaskController")  
@RequestMapping("/quartzTask") 
public class QuartzTaskController {
	
	@Autowired
	private QuartzTaskService quartzTaskService;
	
	private Logger log=LoggerFactory.getLogger(this.getClass());
	
	@ResponseBody
	@RequestMapping(value="/udateOrAddQuartzTask",method=RequestMethod.POST)
    public Map<String,Object> saveQuartzTask(@RequestBody JSONObject json){
		QuartzTask quartzTask=new QuartzTask();
	         quartzTask.setQuartz_name(json.getString("quartz_name"));
	         quartzTask.setQuartz_ano_name(json.getString("quartz_ano_name"));
	         quartzTask.setQuartz_class_path(json.getString("quartz_class_path"));
	         quartzTask.setQuartz_corn(json.getString("quartz_corn"));
	         quartzTask.setQuartz_create_time(json.getDate("quartz_create_time"));
	         quartzTask.setQuartz_abs(json.getString("quartz_abs"));
	         quartzTask.setQuartz_type(json.getString("quartz_type"));
	         quartzTask.setQuartz_id(json.getString("quartz_id"));
	    int update=0;
	    if("*".equals(quartzTask.getQuartz_id())){
	    	update=quartzTaskService.addQuartzTask(quartzTask);
	    }else{
	    	update=quartzTaskService.updateQuartzTask(quartzTask);
	    }
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("result", update>0?"success":"error");
		result.put("message",update<=0?"更新出错！":"");
		return result;
    }
	
	@ResponseBody
	@RequestMapping(value="/delQuartzTask",method=RequestMethod.POST)
    public Map<String,Object>delQuartzTask(@RequestBody JSONObject json){
		QuartzTask quartzTask=new QuartzTask();
		quartzTask.setQuartz_id(json.getString("quartz_id"));
	    int update=quartzTaskService.delQuartzTask(quartzTask);
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("result", update>0?"success":"error");
		result.put("message",update<=0?"更新出错！":"");
		return result;
    }
	
	
	@RequestMapping(value="/manager",method=RequestMethod.GET)
    public ModelAndView manager(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/quartzTask/quartzTaskManager");
        return mv;
    }
	
	@ResponseBody
	@RequestMapping(value="/queryQuartzTaskList",method=RequestMethod.POST)
    public  Map<String,Object> queryQuartzTaskList(HttpServletRequest request){
		 String table = "QUARTZ_TASK";
		 //获取请求次数
	    String draw = "0";
	    draw = request.getParameter("draw");
	    //数据起始位置
	    String start = request.getParameter("start");
	    //数据长度
	    String length = request.getParameter("length");
	    length=String.valueOf(Integer.parseInt(start)+Integer.parseInt(length));
	    start=String.valueOf(Integer.parseInt(start)+1);
	    //总记录数
	    String recordsTotal = "0";
	    //过滤后记录数
	    String recordsFiltered = "";
	    String[] cols = {"","QUARTZ_NAME", "QUARTZ_ANO_NAME", "QUARTZ_CLASS_PATH", "QUARTZ_CORN", "QUARTZ_CREATE_TIME", "QUARTZ_ABS", "QUARTZ_TYPE", "QUARTZ_ID" };
	    //获取客户端需要那一列排序
	    String orderColumn = "0";
	    orderColumn = request.getParameter("order[0][column]");
	    orderColumn = cols[Integer.parseInt(orderColumn)];
	    //获取排序方式 默认为asc
	    String orderDir = "asc";
	    orderDir = request.getParameter("order[0][dir]");
	    
	    //获取用户过滤框里的字符
	    String searchValue = request.getParameter("search[value]");
	    
	    List<String> sArray = new ArrayList<String>();
	    if (!searchValue.equals("")) {
	          sArray.add(" QUARTZ_NAME like '%" + searchValue + "%'");
	          sArray.add(" QUARTZ_ANO_NAME like '%" + searchValue + "%'");
	          sArray.add(" QUARTZ_CLASS_PATH like '%" + searchValue + "%'");
	          sArray.add(" QUARTZ_CORN like '%" + searchValue + "%'");
	          sArray.add(" QUARTZ_CREATE_TIME like '%" + searchValue + "%'");
	          sArray.add(" QUARTZ_ABS like '%" + searchValue + "%'");
	          sArray.add(" QUARTZ_TYPE like '%" + searchValue + "%'");
	          sArray.add(" QUARTZ_ID like '%" + searchValue + "%'");
	    }

	    String individualSearch = "";
	    if (sArray.size() == 1) {
	        individualSearch = sArray.get(0);
	    } else if (sArray.size() > 1) {
	        for (int i = 0; i < sArray.size() - 1; i++) {
	            individualSearch += sArray.get(i) + " or ";
	        }
	        individualSearch += sArray.get(sArray.size() - 1);
	    }

	        String recordsFilteredSql = "select count(1) as recordsFiltered from " + table;
	        //获取数据库总记录数
	        String recordsTotalSql = "select count(1) as recordsTotal from " + table;


	        String searchSQL = "";
	        String sql = "SELECT * FROM (SELECT tt.*, ROWNUM AS rowno"+
                                         " FROM (  SELECT t.* from "+ table+ " t";
	        if (individualSearch != "") {
	            searchSQL = " where " + individualSearch;
	        }
	        sql += searchSQL;
	        recordsFilteredSql += searchSQL;
	        sql += " order by " + orderColumn + " " + orderDir+" )tt WHERE ROWNUM <="+length+") ttt WHERE ttt.rowno >="+start;
	        recordsFilteredSql += " order by " + orderColumn + " " + orderDir;
        
	    return quartzTaskService.getPage(draw,recordsFilteredSql,recordsTotalSql,sql);
    }
}
